package com.iplant.wms.controller.batch;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.batch.MSSBatchProp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/MSSBatchProp")
public class MSSBatchPropController extends BasicController<MSSBatchProp> {

	public MSSBatchPropController() {
		super(MSSBatchProp.class);
	}

	@Autowired
	SMSSService mSMSSService;

	/**
	 * 条件查询
	 */
	@TraceAPI("通过规则查询")
	@GetMapping("/RuleAll")
	public Object RuleAll(HttpServletRequest request) {
		Object wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);
			int wRuleID = StringUtils.parseInt(request.getParameter("RuleID"));
			String wRuleCode = StringUtils.parseString(request.getParameter("RuleCode"));
			ServiceResult<List<MSSBatchProp>> wServiceResult = mSMSSService.MSS_QueryBatchPropList(wLoginUser,wRuleID,wRuleCode);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result,null);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Update")
	public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSBatchProp wMSSBatchProp = CloneTool.Clone(wParam.get("data"), MSSBatchProp.class);
			wMSSBatchProp.setUserInfo(wBMSEmployee);
			ServiceResult<Integer> wServiceResult = mSMSSService.MSS_UpdateBatchProp(wBMSEmployee, wMSSBatchProp);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSBatchProp);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


}
